package common.manager
{
	/**
	 *管理UI视图 
	 *var btn : Button = new Button;
	 *GUI.build(this);
	 *GUI.add("btn",btn);
	 *GUI.setAlgin("btn",GUI.ALIGN_BOTTOM,GUI.ALIGN_RIGHT);
	 *GUI.setOffest("btn",-20,0);
	 *GUI.open("btn");
	 */	
	
	import common.core.GSprite;
	import common.core.Tab;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 界面管理
	 * @author liuyong
	 */	
	public class GUI 
	{		
		static public const ALIGN_MIDDLE : String = "GUI_ALIGN_MIDDLE";
		static public const ALIGN_MIDDLEX : String = "GUI_ALIGN_MIDDLEX";
		static public const ALIGN_MIDDLEY : String = "GUI_ALIGN_MIDDLEY";
		static public const ALIGN_TOP : String = "GUI_ALIGN_TOP";
		static public const ALIGN_BOTTOM : String  = "GUI_ALIGN_BOTTOM";
		static public const ALIGN_LEFT : String = "GUI_ALIGN_LEFT";
		static public const ALIGN_RIGHT : String = "GUI_ALIGN_RIGHT";
		
		static private var m_uiTab : Tab;
		static private var m_uiAlgin : Tab;
		static private var m_uiOffSet : Tab;
		static private var m_uiPop : Tab;
		
		static private var m_root : GSprite;
		
		public function GUI()
		{
		}
		
		static public function build(root:Sprite) : void
		{
			m_uiTab = new Tab;
			m_uiAlgin = new Tab;
			m_uiOffSet = new Tab;
			m_uiPop = new Tab;
			
			m_root = root;
		}
		
		static public function destory() : void
		{
			m_uiTab.destory();
			m_uiAlgin.destory();
			m_uiOffSet.destory();
			
			m_uiTab = 	null;
			m_uiAlgin = null;
			m_uiOffSet = null;
			m_root = 	null;
		}
		
		/**
		 * @param id 号
		 * @param algins 格式
		 */		
		static public function setAlgin(id:String,...algins) : void
		{
			m_uiAlgin.add(id,algins);
		}
		
		static public function setOffest(id:String,offX:Number=0,offY:Number=0) : void
		{
			m_uiOffSet.add(id,{offX:offX,offY:offY});
		}
		
		static public function add(id:String,target:Sprite,pop:Boolean=true) : void
		{
			m_uiTab.add(id,target);
			m_uiPop.add(id,pop);
		}
		
		static public function remove(id:String) : void
		{
			if(isOpen(id))
			{
				close(id);
			}
			
			m_uiTab.remove(id);
			m_uiPop.remove(id);
			
			if(m_uiAlgin.hasItem(id))
			{
				m_uiAlgin.remove(id);
			}
			if(m_uiOffSet.hasItem(id))
			{
				m_uiOffSet.remove(id);
			}
		}
		
		static public function find(id:String) : Sprite
		{
			return m_uiTab.find(id) as Sprite;
		}
		
		static public function has(id : String) : Boolean
		{
			return m_uiTab.hasItem(id);
		}
		
		/**
		 * 销毁所有UI
		 * **/
		static public function removeAll() : void
		{
			for(var id : String in m_uiTab.tab)
			{
				if(m_uiPop.find(id))
				{
					close(id);
				}
			}
		}
		
		/**
		 * 使目标UI到最成为最上层UI
		 * **/
		static public function swap(uiName : String) : void
		{
			var targetUI:Sprite = find(uiName);
			var targetIndex : int = m_root.getChildIndex(targetUI);
			
			if(targetIndex == m_root.numChildren-1 ) return;
			
			var uiGroupA : Array = []; 
			for each(var ui:Sprite in m_uiTab)
			{
				var uiIndex : int = m_root.getChildIndex(ui);
				
				uiGroupA.push({ui:ui,depth:uiIndex});
			} 
			
			uiGroupA.sortOn("depth",Array.NUMERIC);
			
			for(var i:int =0;i<uiGroupA.length;i++)
			{
				uiIndex = uiGroupA[i]["depth"];
				
				if(uiIndex > targetIndex)
				{
					m_root.swapChildrenAt(uiIndex,targetIndex);
				}
			}
		}
		
		/**
		 * 显示UI
		 * **/
		static public function open(uiId : String) : void
		{
			if(has(uiId))
			{	
				var ui : * = find(uiId);
				if(!m_root.contains(ui))
				{
					m_root.addChild(ui);
					
					ui.addEventListener(Event.RENDER,onUIRender,false,0,false);
				}
				else
				{
//					throw new Error("Aready open this UI");
					trace("Aready open this UI");
				}
			}
		}
		
		private static function onUIRender(evt : Event) : void
		{
			evt.stopImmediatePropagation();
			
			var ui : * = evt.currentTarget;
			
			ui.removeEventListener(Event.RENDER,onUIRender);
			
			autoAlgin(ui);
		}
		
		/**
		 * 是否打开了界面
		 * @param uiId
		 * @return  
		 */		
		static public function isOpen(uiId : String) : Boolean
		{
			if(has(uiId))
			{	
				var ui : Sprite = find(uiId);
				if(m_root.contains(ui))
				{
					return true;
				}
			}
			return false;	
		}
		
		/**
		 * 隐藏UI
		 * **/
		static public function close(mediatorName : String) : void
		{
			if(!has(mediatorName))
			{
				trace("already close this facade");
			}
			else
			{
				if(isOpen(mediatorName))
				{
					var ui : * = find(mediatorName);
					m_root.removeChild(ui);
					
//					ui.destory();
//					remove(mediatorName);
				}
			}
		}
		
		/**
		 * 循环显示隐藏UI
		 * **/
		static public function turn(mediatorName : String) : void
		{
			if(has(mediatorName))
			{
				var ui : Sprite = find(mediatorName);
				if(!m_root.contains(ui))
				{
					m_root.addChild(ui);
					autoAlgin(ui);
				}
				else
				{
					m_root.removeChild(ui);
				}
			}
		}
		
		/**
		 * 自适GUI位置格式
		 */		
		static public function autoAlgin(target : DisplayObject = null) : void
		{
			for (var uiId : String in m_uiTab.tab)
			{
				var ui : Sprite = find(uiId);
				
				var canBreak : Boolean;
				
				if(target)
				{
					if(ui == target)
					{
						canBreak = true;
					}
					else
					{
						continue;
					}
				}
				
//				trace(m_root.stage.stageHeight,ui.height);
				
				if(m_uiAlgin.hasItem(uiId))
				{
					for each(var algin : String in m_uiAlgin.find(uiId))
					{
						switch(algin)
						{
							case ALIGN_TOP:
								ui.y=0;
								break;
							case ALIGN_BOTTOM:
								ui.y=m_root.stage.stageHeight - ui.height;
								break;
							case ALIGN_LEFT:
								ui.x=0;
								break;
							case ALIGN_RIGHT:
								ui.x=m_root.stage.stageWidth - ui.width;
								break;
							case ALIGN_MIDDLEY:
								ui.y=(m_root.stage.stageHeight-ui.height)/2;
								break;
							case ALIGN_MIDDLEX:
								ui.x=(m_root.stage.stageWidth-ui.width)/2;
								break;
							case ALIGN_MIDDLE:
								ui.x=(m_root.stage.stageWidth-ui.width)/2;
								ui.y=(m_root.stage.stageHeight-ui.height)/2;
								break;
							default : 
								break;
						}
					}
					
					if(m_uiOffSet.hasItem(uiId))
					{
						var uiOffSet : Object = m_uiOffSet.find(uiId);
						
						ui.x += uiOffSet["offX"];
						ui.y += uiOffSet["offY"];
					}
					
					if(canBreak)
					{
						break;
					}
					
//					trace(ui.x,ui.y);
				}
			}
		}
	}
}